package cn.chenc.framework.core.config;

import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 　@description: 全局时间格式化 转化为标准格式 yyyy-MM-dd HH:mm:ss
 * 　@author 陈_C
 * 　@date 2020/9/2 15:03
 *
 */
@Configuration
public class JacksonObjectMapperConfig {
    private static final String STANDARD_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_PATTERN = "yyyy-MM-dd";
    private static final String TIME_PATTERN = "HH:mm:ss";

    @Bean
    public LocalDateTimeSerializer localDateTimeSerializer() {
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(STANDARD_PATTERN));
    }

    @Bean
    public LocalDateTimeDeserializer localDateTimeDeserializer() {
        return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(STANDARD_PATTERN));
    }

    @Bean
    public LocalDateSerializer localDateSerializer() {
        return new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_PATTERN));
    }

    @Bean
    public LocalDateDeserializer localDateDeserializer() {
        return new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_PATTERN));
    }

    @Bean
    public LocalTimeSerializer localTimeSerializer() {
        return new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_PATTERN));
    }

    @Bean
    public LocalTimeDeserializer localTimeDeserializer() {
        return new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_PATTERN));
    }

    @Bean
    public DateSerializer dateSerializer() {
        return new DateSerializer(false,new SimpleDateFormat(STANDARD_PATTERN));
    }

    @Bean
    public DateDeserializers.DateDeserializer dateDeserializer() {
        return new DateDeserializers.DateDeserializer(new DateDeserializers.DateDeserializer(),new SimpleDateFormat((STANDARD_PATTERN)),null);
    }

    /**
     *  自定义序列化规则
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            //返回时间数据序列化
            builder.serializerByType(Date.class,dateSerializer());
            builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
            builder.serializerByType(LocalDate.class,localDateSerializer());
            builder.serializerByType(LocalTime.class,localTimeSerializer());
            //接收时间数据反序列化
            builder.deserializerByType(Date.class, dateDeserializer());
            builder.deserializerByType(LocalDateTime.class,localDateTimeDeserializer());
            builder.deserializerByType(LocalDate.class,localDateDeserializer());
            builder.deserializerByType(LocalTime.class,localTimeDeserializer());
        };

    }

}
